class ArticlePhotosController < ApplicationController
layout "page"
  def index
    @article = Article.find(params[:article_id])
    @photo_pages = Paginator.new(self, @article.photos.count, 12, params[:page])
    @photos = @article.photos.find(:all, :order => 'created_at DESC',
                                :limit => @photo_pages.items_per_page,
                                :offset => @photo_pages.current.offset)
    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @photos.to_xml }
    end
  end
  
  def new
 #  @article_id = params[:article_id]
    @photo = Photo.new
  end
  
  def create
    @photo = Photo.new(params[:photo])

    respond_to do |format|      
    if Article.find(params[:article_id]).photos << @photo
        flash[:notice] = 'Photo was successfully created.'
            	@imageurl = Imageurl.new
      @imageurl.article_id = params[:article_id]
      @imageurl.simgurl = "http://www.click2edit.com"+@photo.public_filename
      @imageurl.save

        format.html { redirect_to(article_photos_path(:article_id=>params[:article_id])) }
        format.xml  { head :created, 
          :location => article_photo_path(:article_id => @photo.article_id, :id => @photo)}
      else
        format.html { render :action => 'new' }
        format.xml  { render :xml => @photo.errors.to_xml }
      end
    end    
  rescue ActiveRecord::RecordInvalid
    render :action => 'new'
  end
  
    def show
    @photo = Photo.find_by_article_id_and_id(params[:article_id], 
                                          params[:id], 
                                          :include => :article)

    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @photo.to_xml }
    end
  end
  
    def imageedit
    end

  def crop_and_close

    if params[:x1]
      img = Magick::Image::read(params[:imgurl]).first
 
      img.crop!( params[:x1].to_i, params[:y1].to_i, params[:width].to_i, params[:height].to_i, true)
      if (!params[:widt].nil?)&&(!params[:heigt].nil?)
	      img.resize!(params[:widt].to_i, params[:heigt].to_i)
	    else
	    	      img.resize!(params[:width].to_i, params[:height].to_i)
	    end
	    
      img.rotate!(params[:rotang].to_i)
			
			extnimgurl = params[:imgurl].split('/').last 
			extens = extnimgurl.split('.').last 
			
      urlpath = params[:imgurl][0,params[:imgurl].rindex('/')]+"/"
	 
			@imagurla = urlpath+Date.today.to_s.split('-').join('')+Time.now.to_i.to_s+"_"+rand(10000).to_s+"."+extens

      img.write(@imagurla)
      
      @imagurla = "http://www.click2edit.com/"+@imagurla[@imagurla.index('/')+1,@imagurla.length-@imagurla.index('/')-1]

      
       @currimg = Imageurl.new
       @currimg.simgurl = @imagurla
       @currimg.article_id = params[:article_id3]
       @currimg.save
    end
    redirect_to :action => 'index' ,:article_id=>params[:article_id3]
  end
  
    def edit
    @photo = Article.find(params[:article_id]).photos.find(params[:id])    
  rescue ActiveRecord::RecordNotFound
    redirect_to :action => 'index'    
  end
  
    def update
    @photo = Article.find(params[:article_id]).photos.find(params[:id])

    respond_to do |format|
      if @photo.update_attributes(params[:photo])
        flash[:notice] = 'Photo was successfully updated.'
        format.html { redirect_to article_photo_path(:article_id => Article.find(params[:article_id]), 
                                                  :id => @photo) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @photo.errors.to_xml }
      end
    end
  rescue ActiveRecord::RecordNotFound
    redirect_to :action => 'index'  
  end

  def destroy
    @photo = Article.find(params[:article_id]).photos.find(params[:id])
    @photo.destroy

    respond_to do |format|
      format.html { redirect_to article_photos_path }
      format.xml  { head :ok }
    end
  rescue ActiveRecord::RecordNotFound
    redirect_to :action => 'index'    
  end

  def destroy1
    @imageurl = Imageurl.find(params[:id])
    @filedel = Dir.pwd+"/public/articlesimages/"+(@imageurl.article_id).to_s+"/"+@imageurl.simgurl.split('/').last    
    File.delete(@filedel)if File.exist?(@filedel)
    if @imageurl.destroy
      flash[:notice] = "imageurl deleted"
    else
      flash[:error] = "There was a problem deleting this imageurl."
    end

    redirect_to :action => 'index',:article_id=>params[:article_id]
  end
  
end
